﻿using GalaSoft.MvvmLight;

namespace Budgetsch.WP7.ViewModel.Public
{
    using System.Collections.ObjectModel;
    using System.Windows.Input;

    using Budgetsch.WP7.Navigation;
    using Budgetsch.WP7.ViewModel.Cost;
    using Budgetsch.WP7.ViewModel.Income;
    using Budgetsch.WP7.ViewModel.Saving;
    using Budgetsch.WP7.WP7DataService;

    using GalaSoft.MvvmLight.Command;
    using GalaSoft.MvvmLight.Messaging;

    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class PublicDashboardViewModel : ViewModelBase
    {
        private readonly INavigationService _navigationService;

        /// <summary>
        /// The <see cref="User" /> property's name.
        /// </summary>
        public const string UserPropertyName = "User";

        private User _user;

        /// <summary>
        /// Sets and gets the User property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public User User
        {
            get
            {
                return _user;
            }

            set
            {
                if (_user == value)
                {
                    return;
                }

                _user = value;

                RaisePropertyChanged(UserPropertyName);
            }
        }

        /// <summary>
        /// The <see cref="IncomeListDetailsViewModels" /> property's name.
        /// </summary>
        public const string IncomeListDetailsViewModelsPropertyName = "IncomeListDetailsViewModels";

        private ObservableCollection<IncomeListDetailsViewModel> _incomeListDetailsViewModels = new ObservableCollection<IncomeListDetailsViewModel>();

        /// <summary>
        /// Sets and gets the IncomeListDetailsViewModels property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public ObservableCollection<IncomeListDetailsViewModel> IncomeListDetailsViewModels
        {
            get
            {
                return _incomeListDetailsViewModels;
            }

            set
            {
                if (_incomeListDetailsViewModels == value)
                {
                    return;
                }

                _incomeListDetailsViewModels = value;
                RaisePropertyChanged(IncomeListDetailsViewModelsPropertyName);
            }
        }

        /// <summary>
        /// The <see cref="CostListDetailsViewModels" /> property's name.
        /// </summary>
        public const string CostListDetailsViewModelsPropertyName = "CostListDetailsViewModels";

        private ObservableCollection<CostListDetailsViewModel> _costListDetailsViewModels = new ObservableCollection<CostListDetailsViewModel>();

        /// <summary>
        /// Sets and gets the CostListDetailsViewModels property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public ObservableCollection<CostListDetailsViewModel> CostListDetailsViewModels
        {
            get
            {
                return _costListDetailsViewModels;
            }

            set
            {
                if (_costListDetailsViewModels == value)
                {
                    return;
                }

                _costListDetailsViewModels = value;
                RaisePropertyChanged(CostListDetailsViewModelsPropertyName);
            }
        }

        /// <summary>
        /// The <see cref="SavingListDetailsViewModels" /> property's name.
        /// </summary>
        public const string SavingListDetailsViewModelsPropertyName = "SavingListDetailsViewModels";

        private ObservableCollection<SavingListDetailsViewModel> _savingListDetailsViewModels = new ObservableCollection<SavingListDetailsViewModel>();

        /// <summary>
        /// Sets and gets the SavingListDetailsViewModels property.
        /// Changes to that property's value raise the PropertyChanged event. 
        /// </summary>
        public ObservableCollection<SavingListDetailsViewModel> SavingListDetailsViewModels
        {
            get
            {
                return _savingListDetailsViewModels;
            }

            set
            {
                if (_savingListDetailsViewModels == value)
                {
                    return;
                }

                _savingListDetailsViewModels = value;
                RaisePropertyChanged(SavingListDetailsViewModelsPropertyName);
            }
        }

        public ICommand IncomeNavigationCommand { get; set; }
        public ICommand CostNavigationCommand { get; set; }
        public ICommand SavingNavigationCommand { get; set; }


        /// <summary>
        /// Initializes a new instance of the PublicDashboardViewModel class.
        /// </summary>
        public PublicDashboardViewModel(INavigationService navigationService)
        {
            _navigationService = navigationService;

            IncomeNavigationCommand = new RelayCommand<IncomeListDetailsViewModel>(p =>
            {
                if (p != null)
                { p.NavigateToDetailsView(); }
            });
            CostNavigationCommand = new RelayCommand<CostListDetailsViewModel>(p =>
                {
                    if (p != null)
                    {
                        p.NavigateToDetailsView();
                    }
                });
            SavingNavigationCommand = new RelayCommand<SavingListDetailsViewModel>(p =>
            {
                if (p != null)
                {
                    p.NavigateToDetailsView();
                }
            });

            Messenger.Default.Register<LoadPublicDashboardViewMessage>(this, m =>
                {
                    User = m.User;

                    IncomeListDetailsViewModels.Clear();
                    CostListDetailsViewModels.Clear();
                    SavingListDetailsViewModels.Clear();

                    foreach (var income in User.Incomes)
                    {
                        IncomeListDetailsViewModels.Add(new IncomeListDetailsViewModel(income, _navigationService));
                    }

                    foreach (var cost in User.Costs)
                    {
                        CostListDetailsViewModels.Add(new CostListDetailsViewModel(cost, _navigationService));
                    }

                    foreach (var saving in User.Savings)
                    {
                        SavingListDetailsViewModels.Add(new SavingListDetailsViewModel(saving, _navigationService));
                    }


                });
        }
    }
}